package com.yc.tomcat.impl;

import com.yc.tomcat.HttpServletRequest;
import com.yc.tomcat.HttpSession;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class HttpSessionImpl implements HttpSession {

    long creationTime = System.currentTimeMillis();
    String id = UUID.randomUUID().toString();
    Map<String,Object> attributeMap = new HashMap<>();

    @Override
    public long getCreationTime() {
        return creationTime;
    }

    @Override
    public String getId() {
        return id;
    }

    @Override
    public long getLastAccessedTime() {
        return 0;
    }

    @Override
    public int getMaxInactiveInterval() {
        return 30;
    }

    @Override
    public void invalidate() {
        // 从容器中移除会话对象
        HttpServletRequestImpl.sessionMap.remove(this.id);
    }

    @Override
    public void setAttribute(String name, Object value) {
        attributeMap.put(name,value);
    }

    @Override
    public Object getAttribute(String name) {
        return attributeMap.get(name);
    }

    @Override
    public void removeAttribute(String name) {
        attributeMap.remove(name);
    }
}
